cmake_minimum_required(VERSION 3.16)

project(FileScannerUI)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找Qt5
find_package(Qt5 REQUIRED COMPONENTS Core Widgets)

# 设置Qt5的MOC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 包含目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../Common)

# 源文件
set(SOURCES
    main.cpp
    ScannerUI.cpp
)

set(HEADERS
    ScannerUI.h
    ../Common/ScannerInterface.h
)

# 创建可执行文件
add_executable(FileScannerUI ${SOURCES} ${HEADERS})

# 链接Qt5库
target_link_libraries(FileScannerUI 
    Qt5::Core 
    Qt5::Widgets
)

# Windows特定设置
if(WIN32)
    # 链接Windows库
    target_link_libraries(FileScannerUI)
    
    # 设置Windows应用程序属性
    set_target_properties(FileScannerUI PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
    
    # 复制DLL到输出目录
    add_custom_command(TARGET FileScannerUI POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:FileScannerDLL>
        $<TARGET_FILE_DIR:FileScannerUI>
    )
else()
    # Linux设置
    target_link_libraries(FileScannerUI pthread dl)
    
    # 设置RPATH以便找到共享库
    set_target_properties(FileScannerUI PROPERTIES
        INSTALL_RPATH "$ORIGIN"
        BUILD_WITH_INSTALL_RPATH TRUE
    )
endif()

# 编译选项
if(MSVC)
    target_compile_options(FileScannerUI PRIVATE /W4)
    # 设置为Windows应用程序（不显示控制台）
    set_target_properties(FileScannerUI PROPERTIES
        LINK_FLAGS "/SUBSYSTEM:WINDOWS"
    )
else()
    target_compile_options(FileScannerUI PRIVATE -Wall -Wextra -pthread)
endif()

# 安装规则
install(TARGETS FileScannerUI
    RUNTIME DESTINATION bin
)

# 如果是Windows，安装Qt5运行时
if(WIN32)
    # 查找Qt5部署工具
    get_target_property(QT_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
    get_filename_component(QT_WINDEPLOYQT_EXECUTABLE ${QT_QMAKE_EXECUTABLE} PATH)
    set(QT_WINDEPLOYQT_EXECUTABLE "${QT_WINDEPLOYQT_EXECUTABLE}/windeployqt.exe")
    
    if(EXISTS ${QT_WINDEPLOYQT_EXECUTABLE})
        add_custom_command(TARGET FileScannerUI POST_BUILD
            COMMAND ${QT_WINDEPLOYQT_EXECUTABLE} $<TARGET_FILE:FileScannerUI>
            COMMENT "Deploying Qt libraries")
    endif()
endif()